package com.xuecheng.content.service.impl;

import cn.hutool.core.io.IoUtil;
import com.xuecheng.content.config.MinioTemplate;
import freemarker.template.Configuration;
import freemarker.template.Template;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;

import java.io.ByteArrayInputStream;
import java.util.Map;

@Service // 课程发布  freemarker +minio
public class AsyncCoursePubService {


    @Autowired
    private Configuration configuration;

    @Autowired
    private MinioTemplate minioTemplate;


    // 异步调用
    @Async
    public void publish(Map map ,Long courseBaseId) throws Exception {
        Thread.sleep(3000);
        // 6.生成html，保存到minio
        // 6-1 加载指定模板
        Template template = configuration.getTemplate("learing_article.ftl");
        // 6-2 模板+模型 = 输出
        String content = FreeMarkerTemplateUtils.processTemplateIntoString(template, map);
        // 6-3 文本内容转成io流
        ByteArrayInputStream inputStream = IoUtil.toStream(content.getBytes());

        // 7.保存minio中    文件名  47.html
        minioTemplate.uploadHtml(inputStream,courseBaseId+".html");
    }
}